package org.apache.hadoop.hbase.my.example;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;

public class HBaseTest {
  public static void main(String[] args) {
    try {
      // TODO_MY: 2022/2/24 从resources加载hbase-default.xml和hbase-site.xml配置文件
      Configuration conf = HBaseConfiguration.create();
      // TODO_MY: 2022/2/24 实例化ConnectionImplementation对象
      Connection connection = ConnectionFactory.createConnection(conf);

      // TODO_MY: 2022/2/24 ConnectionImplementation对象的getAdmin和getTable方法
      Admin admin = connection.getAdmin();
      Table table = connection.getTable(TableName.valueOf("test"));  // 返回HTable对象

      // TODO_MY: 2022/2/24 创建一个表
      TableDescriptorBuilder descBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("test"));
      ColumnFamilyDescriptor cfDesc = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build();
      TableDescriptor desc = descBuilder.setColumnFamily(cfDesc).build();
      admin.createTable(desc);

      // TODO_MY: 2022/2/24 新增一行数据到HBase，调用HTable的put方法
      Put put = new Put(Bytes.toBytes("a"));
      put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
      table.put(put);

      // TODO_MY: 2022/2/24 从HBase获取一行数据
      Get get = new Get(Bytes.toBytes("a"));
      table.get(get);

      // TODO_MY: 2022/2/24 扫描一张表的所有数据
      Scan scan = new Scan();
      table.getScanner(scan);

      // TODO_MY: 2022/2/24 删除一行数据
      Delete delete = new Delete(Bytes.toBytes("a"));
      table.delete(delete);

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
